using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class NotificationsManager
{
	public enum NotificationName
	{
		None = 0,
		DailyReward = 1,
		Facebook = 2,
		Twitter = 3,
		RewardedAd = 4
	}

	public class Notification
	{
		private const char valueSeparator = ';';

		private const char arraySeperator = '|';

		public NotificationName name;

		public List<TimeSpan> cooldowns;

		public int dailyLimit = -1;

		public int reward;

		public bool available;

		public int currentCooldown;

		public int dailyCount;

		public DateTime lastCompleted = default(DateTime);

		public bool everAvailable;

		public bool everCollected;

		public bool everClosedOnce;

		public bool everClosedTwice;

		public bool everCheated;

		public Notification()
		{
		}

		public Notification(NotificationName name_, List<TimeSpan> cooldowns_, int dailyLimit_, int reward_, int currentCooldown_ = 0, int dailyCount_ = 0)
		{
			name = name_;
			cooldowns = cooldowns_;
			dailyLimit = dailyLimit_;
			reward = reward_;
			currentCooldown = currentCooldown_;
			dailyCount = dailyCount_;
		}

		public void Overwrite(Notification newNotification)
		{
			name = newNotification.name;
			cooldowns = newNotification.cooldowns;
			dailyLimit = newNotification.dailyLimit;
			reward = newNotification.reward;
		}

		public string Encode()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(name.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(Utils.EncodeTimeSpanList(cooldowns));
			stringBuilder.Append(';');
			stringBuilder.Append(dailyLimit.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(reward.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(available.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(currentCooldown.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(dailyCount.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(lastCompleted.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(everAvailable.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(everCollected.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(everClosedOnce.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(everClosedTwice.ToString());
			stringBuilder.Append(';');
			stringBuilder.Append(everCheated.ToString());
			return stringBuilder.ToString();
		}

		public bool Decode(string encoded)
		{
			if (encoded == null || encoded == string.Empty || encoded == string.Empty)
			{
				Debug.Log("Notification->Decode : encoded is empty");
				return false;
			}
			string[] array = encoded.Split(';');
			if (array == null)
			{
				Debug.Log("Notification->Decode : values is null");
				return false;
			}
			int num = 0;
			if (num < array.Length)
			{
				try
				{
					name = (NotificationName)(int)Enum.Parse(typeof(NotificationName), array[num]);
				}
				catch (ArgumentException)
				{
					Debug.Log("Notification->Decode : could not decode name");
					return false;
				}
				num++;
				if (num < array.Length)
				{
					if (!Utils.DecodeTimeSpanList(array[num], out cooldowns))
					{
						Debug.Log("Notification->Decode : could not decode cooldowns");
						return false;
					}
					num++;
					if (num < array.Length)
					{
						if (!int.TryParse(array[num], out dailyLimit))
						{
							Debug.Log("Notification->Decode : could not decode dailyLimit");
							return false;
						}
						num++;
						if (num < array.Length)
						{
							if (!int.TryParse(array[num], out reward))
							{
								Debug.Log("Notification->Decode : could not decode reward");
								return false;
							}
							num++;
							if (num < array.Length && !bool.TryParse(array[num], out available))
							{
								Debug.Log("Notification->Decode : could not decode available");
								return false;
							}
							num++;
							if (num < array.Length && !int.TryParse(array[num], out currentCooldown))
							{
								Debug.Log("Notification->Decode : could not decode currentCooldown");
								return false;
							}
							num++;
							if (num < array.Length && !int.TryParse(array[num], out dailyCount))
							{
								Debug.Log("Notification->Decode : could not decode dailyCount");
								return false;
							}
							num++;
							if (num < array.Length && !DateTime.TryParse(array[num], out lastCompleted))
							{
								Debug.Log("Notification->Decode : could not decode lastCompleted");
								return false;
							}
							num++;
							if (num < array.Length && !bool.TryParse(array[num], out everAvailable))
							{
								Debug.Log("Notification->Decode : could not decode everAvailable");
								return false;
							}
							num++;
							if (num < array.Length && !bool.TryParse(array[num], out everCollected))
							{
								Debug.Log("Notification->Decode : could not decode everCollected");
								return false;
							}
							num++;
							if (num < array.Length && !bool.TryParse(array[num], out everClosedOnce))
							{
								Debug.Log("Notification->Decode : could not decode everClosedOnce");
								return false;
							}
							num++;
							if (num < array.Length && !bool.TryParse(array[num], out everClosedTwice))
							{
								Debug.Log("Notification->Decode : could not decode everClosedTwice");
								return false;
							}
							num++;
							if (num < array.Length && !bool.TryParse(array[num], out everCheated))
							{
								Debug.Log("Notification->Decode : could not decode everCheated");
								return false;
							}
							return true;
						}
						Debug.Log("Notification->Decode : mandatory value not found at index " + num);
						return false;
					}
					Debug.Log("Notification->Decode : mandatory value not found at index " + num);
					return false;
				}
				Debug.Log("Notification->Decode : mandatory value not found at index " + num);
				return false;
			}
			Debug.Log("Notification->Decode : mandatory value not found at index " + num);
			return false;
		}

		public override string ToString()
		{
			string text = "\n    Notification:";
			text = text + "\n\t name: " + name;
			text += "\n\t cooldowns:";
			if (cooldowns != null)
			{
				foreach (TimeSpan cooldown in cooldowns)
				{
					text = text + cooldown.ToString() + ", ";
				}
			}
			else
			{
				text += "NULL";
			}
			char[] trimChars = new char[2] { ',', ' ' };
			text.TrimEnd(trimChars);
			text = text + "\n\t dailyLimit: " + dailyLimit;
			text = text + "\n\t reward: " + reward;
			text += "\n\t\t USER DATA:";
			text = text + "\n\t\t available: " + available;
			text = text + "\n\t\t currentCooldown: " + currentCooldown;
			text = text + "\n\t\t dailyCount: " + dailyCount;
			text = text + "\n\t\t lastCompleted: " + lastCompleted.ToString();
			text = text + "\n\t\t everAvailable: " + everAvailable;
			text = text + "\n\t\t everCollected: " + everCollected;
			text = text + "\n\t\t everClosedOnce: " + everClosedOnce;
			text = text + "\n\t\t everClosedTwice: " + everClosedTwice;
			text = text + "\n\t\t everCheated: " + everCheated;
			return text + "\n ";
		}

		public TimeSpan GetCooldown()
		{
			if (!everCollected && name == NotificationName.DailyReward)
			{
				return TimeSpan.FromMinutes(15.0);
			}
			return cooldowns[currentCooldown];
		}

		public bool IsReady()
		{
			if (dailyLimit > 0 && dailyCount >= dailyLimit)
			{
				return false;
			}
			if (currentCooldown >= cooldowns.Count)
			{
				currentCooldown = 0;
			}
			if (cooldowns.Count > 0)
			{
				int num = DateTime.UtcNow.CompareTo(lastCompleted.Add(GetCooldown()));
				if (num < 0)
				{
					return false;
				}
			}
			return true;
		}

		public void Complete()
		{
			available = false;
			CheckForDailyLimitReset();
			dailyCount++;
			lastCompleted = DateTime.UtcNow;
			currentCooldown = 0;
		}

		public void Dismiss()
		{
			available = false;
			currentCooldown++;
			if (currentCooldown >= cooldowns.Count)
			{
				currentCooldown = 0;
			}
			lastCompleted = DateTime.UtcNow;
		}

		public int GetRemainingSeconds()
		{
			if (cooldowns.Count > 0)
			{
				return Mathf.FloorToInt((float)(lastCompleted.Add(GetCooldown()) - DateTime.UtcNow).TotalSeconds);
			}
			return 0;
		}

		public void CheckForDailyLimitReset()
		{
			if (dailyCount != 0 && !(lastCompleted == default(DateTime)))
			{
				DateTime date = DateTime.UtcNow.Date;
				if (lastCompleted.Date != date && lastCompleted.Date.CompareTo(date) < 0)
				{
					dailyCount = 0;
				}
			}
		}
	}

	public static Dictionary<NotificationName, Notification> defaultNotifications = new Dictionary<NotificationName, Notification>
	{
		{
			NotificationName.DailyReward,
			new Notification(NotificationName.DailyReward, new List<TimeSpan>
			{
				new TimeSpan(1, 0, 0, 0)
			}, -1, 20)
		},
		{
			NotificationName.Facebook,
			new Notification(NotificationName.Facebook, new List<TimeSpan>
			{
				new TimeSpan(2, 0, 0, 0),
				new TimeSpan(3, 0, 0, 0),
				new TimeSpan(7, 0, 0, 0)
			}, -1, 10)
		},
		{
			NotificationName.Twitter,
			new Notification(NotificationName.Twitter, new List<TimeSpan>
			{
				new TimeSpan(2, 0, 0, 0),
				new TimeSpan(3, 0, 0, 0),
				new TimeSpan(7, 0, 0, 0)
			}, -1, 10)
		},
		{
			NotificationName.RewardedAd,
			new Notification(NotificationName.RewardedAd, new List<TimeSpan>
			{
				new TimeSpan(0, 0, 1, 0),
				new TimeSpan(0, 0, 30, 0),
				new TimeSpan(2, 0, 0, 0)
			}, 10, 5)
		}
	};

	private Dictionary<NotificationName, Notification> notifications;

	public List<NotificationName> activeNotifications;

	public NotificationsManager()
	{
		activeNotifications = new List<NotificationName>();
	}

	public static void ScheduleDailyRewardLocalNotification(DateTime fireDate)
	{
	}

	public static void CancelAllLocalNotifications()
	{
	}

	public static void ResetAppBadgeIcon()
	{
	}

	public static void RegisterForLocalNotification()
	{
	}

	public void LoadSettings(EconomyVariableSettings settings)
	{
		if (settings == null || settings.notifications == null)
		{
			return;
		}
		notifications = new Dictionary<NotificationName, Notification>();
		foreach (KeyValuePair<NotificationName, Notification> notification in settings.notifications)
		{
			notifications.Add(notification.Key, notification.Value);
		}
	}

	public bool RefreshActiveNotifications()
	{
		bool result = false;
		List<NotificationName> list = new List<NotificationName>();
		foreach (NotificationName activeNotification in activeNotifications)
		{
			list.Add(activeNotification);
		}
		activeNotifications.Clear();
		foreach (KeyValuePair<NotificationName, Notification> notification in notifications)
		{
			if (notification.Value.available)
			{
				if (notification.Key != NotificationName.DailyReward && !activeNotifications.Contains(notification.Key))
				{
					activeNotifications.Add(notification.Key);
					if (!list.Contains(notification.Key))
					{
						result = true;
					}
				}
			}
			else
			{
				if (!IsReadyAndAvailable(notification.Key))
				{
					continue;
				}
				notification.Value.available = true;
				if (notification.Key != NotificationName.DailyReward && !activeNotifications.Contains(notification.Key))
				{
					activeNotifications.Add(notification.Key);
					if (!list.Contains(notification.Key))
					{
						result = true;
					}
				}
				CustomAnalytics.LogNotificationsEvent("Count", notification.Key.ToString() + " - Available");
				if (!notification.Value.everAvailable)
				{
					notification.Value.everAvailable = true;
					CustomAnalytics.LogNotificationsEvent("User Count", notification.Key.ToString() + " - Available");
				}
			}
		}
		if (list.Count != activeNotifications.Count)
		{
			result = true;
		}
		return result;
	}

	public int GetActiveNotificationsCount()
	{
		int num = activeNotifications.Count;
		if (notifications[NotificationName.DailyReward].IsReady())
		{
			num++;
			if (!notifications[NotificationName.DailyReward].available)
			{
				notifications[NotificationName.DailyReward].available = true;
				CustomAnalytics.LogNotificationsEvent("Count", NotificationName.DailyReward.ToString() + " - Available");
				if (!notifications[NotificationName.DailyReward].everAvailable)
				{
					notifications[NotificationName.DailyReward].everAvailable = true;
					CustomAnalytics.LogNotificationsEvent("User Count", NotificationName.DailyReward.ToString() + " - Available");
				}
			}
		}
		return num;
	}

	public bool CheckForCheating()
	{
		bool result = false;
		foreach (KeyValuePair<NotificationName, Notification> notification in notifications)
		{
			int num = DateTime.UtcNow.CompareTo(notification.Value.lastCompleted);
			if (num < 0)
			{
				result = true;
				notification.Value.available = false;
				notification.Value.lastCompleted = DateTime.UtcNow;
				CustomAnalytics.LogNotificationsEvent("Count", notification.Key.ToString() + " - Cheat");
				if (!notification.Value.everCheated)
				{
					notification.Value.everCheated = true;
					CustomAnalytics.LogNotificationsEvent("User Count", notification.Key.ToString() + " - Cheat");
				}
			}
		}
		return result;
	}

	public bool IsReadyAndAvailable(NotificationName notificationName)
	{
		switch (notificationName)
		{
		case NotificationName.Facebook:
			return false;
		case NotificationName.Twitter:
			return false;
		case NotificationName.RewardedAd:
			Debug.Log(string.Concat(notificationName, " is ready : Economy.isRewardedAdAvailable= ", Economy.isRewardedAdAvailable));
			if (Economy.GetTotalPlayTime() < 600)
			{
				return false;
			}
			if (!Economy.isRewardedAdAvailable)
			{
				return false;
			}
			break;
		}
		return notifications[notificationName].IsReady();
	}

	public void Complete(NotificationName notificationName)
	{
		notifications[notificationName].Complete();
		activeNotifications.Remove(notificationName);
		CustomAnalytics.LogNotificationsEvent("Count", notificationName.ToString() + " - Collected");
		if (!notifications[notificationName].everCollected)
		{
			notifications[notificationName].everCollected = true;
			CustomAnalytics.LogNotificationsEvent("User Count", notificationName.ToString() + " - Collected");
		}
	}

	public void Dismiss(NotificationName notificationName)
	{
		notifications[notificationName].Dismiss();
		activeNotifications.Remove(notificationName);
		if (notifications[notificationName].currentCooldown == 1)
		{
			CustomAnalytics.LogNotificationsEvent("Count", notificationName.ToString() + " - Closed Once");
			if (!notifications[notificationName].everClosedOnce)
			{
				notifications[notificationName].everClosedOnce = true;
				CustomAnalytics.LogNotificationsEvent("User Count", notificationName.ToString() + " - Closed once");
			}
		}
		if (notifications[notificationName].currentCooldown == 2)
		{
			CustomAnalytics.LogNotificationsEvent("Count", notificationName.ToString() + " - Closed Twice");
			if (!notifications[notificationName].everClosedTwice)
			{
				notifications[notificationName].everClosedTwice = true;
				CustomAnalytics.LogNotificationsEvent("User Count", notificationName.ToString() + " - Closed Twice");
			}
		}
	}

	public Notification GetNotification(NotificationName notificationName)
	{
		if (notifications.ContainsKey(notificationName))
		{
			return notifications[notificationName];
		}
		Debug.Log(string.Concat("WTF : ", notificationName, " not found for NotificationsManager->GetNotification"));
		return null;
	}

	public int GetRemainingSeconds(NotificationName notificationName)
	{
		return notifications[notificationName].GetRemainingSeconds();
	}

	public int GetReward(NotificationName notificationName)
	{
		return notifications[notificationName].reward;
	}
}
